// list.js
var app = getApp()
var lastClickTime = 0
var minClickTime = app.globalData.minClickTime
var encryptUtils = require('../../../utils/HHEncryptUtils.js')
var modelUtils = require('../../../utils/HHModelUtils.js')
var baseImgPath = app.globalData.imgBasepath
var pageSize = app.globalData.pageSize
var pageIndex = 1
var loading = false;

var pageConfirm = []//保存每一加载信息
var currentIndex = 0


Page({

   /**
    * 页面的初始数据  table 标签可自行添加
    */
   data: {
      table: [
         { 'name': "未使用", 'mark': "1", 'isSelect': true },
         { 'name': "已使用", 'mark': "2", 'isSelect': false },
         { 'name': "已过期", 'mark': "3", 'isSelect': false }
      ],
      loading: true,
      complate: false,
      noData: false,
      loadMore: false,
      noDataMsg: "暂无数据",
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function (options) {
      pageConfirm = []
      loading = false
      currentIndex = 0
      var table = this.data.table
      for (var i = 0; i < table.length; i++) {
         var pageConfirmItem = {}
         pageConfirmItem["page"] = 1
         pageConfirmItem["pageCount"] = 0
         pageConfirmItem["mark"] = table[i].mark
         pageConfirmItem["list"] = []
         pageConfirmItem["isGet"] = false
         pageConfirm = pageConfirm.concat(pageConfirmItem)
      }

      this.getPageData()
   },

   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function () {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function () {

   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function () {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function () {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {
      var pageComnfimItem = pageConfirm[currentIndex]
      pageComnfimItem["page"] = 1
      this.getPageData()
   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function () {
      this.getMoreData()
   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function () {

   },
   getPageData: function () {
      loading = true
      var para = {}
      para["page_size"] = encryptUtils.base64Encode(pageSize + "")
      para["state"] = encryptUtils.base64Encode(pageConfirm[currentIndex].mark + "")
      para["page"] = encryptUtils.base64Encode(pageConfirm[currentIndex].page + "")
      // 自行添加参数


      var str = JSON.stringify(para)
      console.log("para str==" + str)
      var urlStr = encodeURIComponent(str)

      console.log("para==" + urlStr)
      var that = this
      wx.request({
         url: app.globalData.ip + "couponlist",
         method: 'POSt',
         header: {
            'From': "WeChat"
         },
         data: {
            'para': urlStr
         },
         success: function (res) {

            var code = res.data.code
            console.log("result==" + JSON.stringify(res.data))
            var pageConfirmItem = pageConfirm[currentIndex]
            var pageIndex = pageConfirmItem["page"]
            if (code == 100) {

               var needData = res.data.result
               var tempList = modelUtils.getList(needData)
               pageConfirmItem["pageCount"] = tempList.length
               var loadMore = tempList.length == pageSize

               if (pageIndex == 1) {
                  pageConfirmItem["isGet"] = true
                  pageConfirmItem["list"] = tempList
                  that.setData(
                     {
                        list: tempList,
                        loading: false,
                        complate: true,
                        noData: false,
                        loadMore: loadMore
                     }
                  )
               } else {
                  var list = pageConfirmItem.list
                  list = list.concat(tempList)
                  pageConfirmItem["list"] = list
                  that.setData(
                     {
                        list: list,
                        loadMore: loadMore
                     }
                  )

               }

            } else {


               if (pageIndex == 1) {

                  pageConfirmItem["isGet"] = true
                  pageConfirmItem["pageCount"] = 0
                  pageConfirmItem["list"] = []
                  that.setData(
                     {
                        list: [],
                        loading: false,
                        complate: true,
                        noData: true,
                        noDataMsg: "暂无数据"
                     }
                  )

               } else {

                   var message = res.data.msg
                  wx.showToast({
                     title: message,
                  })
                  if (code == 101) {
                     that.setData(
                        {
                           loadMore: false
                        }
                     )
                  }

               }

            }

         },

         fail: function (res) {


            if (pageIndex == 1) {
               that.setData(
                  {
                     loading: false,
                     complate: true,
                     noData: true,
                     noDataMsg: "加载失败，请刷新重试"
                  }
               )
            } else {
               wx.showToast({
                  title: '网络异常',
                  icon: 'success',

               })
            }
         }
         ,
         complete: function () {
            loading = false
            wx.stopPullDownRefresh()
            if (wx.hideLoading) {
               wx.hideLoading()
            }

         }
      }


      )

   },
   tableItemClick: function (event) {
      var index = event.currentTarget.id
      currentIndex = index
      var table = this.data.table
      for (var i = 0; i < table.length; i++) {
         if (i == index) {
            table[i]["isSelect"] = true
         } else {
            table[i]["isSelect"] = false
         }
      }

      var pageComnfimItem = pageConfirm[currentIndex]
      var list = pageComnfimItem.list
      var isGet = pageComnfimItem.isGet
      if (list.length == 0 && !isGet) {
         this.setData(
            {
               table: table,
               loading: true,
               complate: false,
               noData: false,
               loadMore: false,
            }
         )
         this.getPageData()
      } else {
         var pageCount = pageComnfimItem["pageCount"]
         this.setData(
            {
               list: list,
               table: table,
               loading: false,
               complate: true,
               noData: list.length <= 0,
               loadMore: pageCount == pageSize
            }
         )

      }


   },

   getMoreData: function () {

      var loadMore = this.data.loadMore

      if (loadMore && !loading) {
         var pageComnfimItem = pageConfirm[currentIndex]
         var pageIndex = pageComnfimItem["page"]
         pageIndex += 1
         console.log("pageIndex==" + pageIndex)
         pageComnfimItem["page"] = pageIndex
         this.getPageData()
      }

   },


})